layui.use(['table','form', 'laydate'], function() {
    var table = layui.table
        , form = layui.form
        , $ = layui.$;
    let courseData = JSON.parse(sessionStorage.getItem('courseData'));
    //总课表
    table.render({
        elem: '#changeTable',
        height: 'full-98',
        url: '/Class/table/getChangeTables', //数据接口
        title: '总课表',
        page: true ,//开启分页
        limits:[10,20,30,40],
        where: {"year": courseData.year,"term": courseData.term,"time": courseData.time ? courseData.time : new Date()}, //传入当前要调整课程的学年，学期，时间
        cols:
            [[ //表头
                { field: 'id',title: 'ID',sort: true,hide: true},
                { field: 'courseId',title: '课程', width: 160},
                { field: 'classId',title: '班级'},
                { field: 'roomId',title: '教室'},
                { field: 'userId',title: '教师'},
                { field: 'schoolYear',title: '学年'},
                { field: 'schoolTerm',title: '学期'},
                { field: 'week',title: '周数', sort: true},
                { field: 'weekNum',title: '周几', sort: true},
                { field: 'timeSlot',title: '节数', sort: true},
                { field: 'createTime',title: '上课时间', sort: true, templet: (d) =>{
                      return utils.formatDate(d.createTime)
                    }},
            ]]
    });
    //进行模糊查询 (用户名和类型)
    form.on('submit(search)', function(data){
        table.reload('changeTable', {
            url:'/Class/table/getChangeTables',
            where: { //设定异步数据接口的额外参数，任意设
                "courseName":data.field.courseName,
                "realName":data.field.userName,
                "roomCode":data.field.roomCode,
                "classCode":data.field.classCode,
                "year":courseData.year,
                "term":parseInt(courseData.term),
                "time":courseData.time
            }
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        }); //只重载数据
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
    //监听行双击事件
    table.on('rowDouble(changeTable)', function(obj){
        //检查课程能否调整
        $.ajax({
            type: 'post',
            url: '/Class/apply/check',
            dataType: 'json',
            data: { "id": obj.data.id, "type": parseInt(1)},
            success: (res) =>{
                if(res.code !== 0){
                    layer.msg(res.msg);
                }else{
                    layer.msg('是否选中此项？', {
                        time: 20000, //20s后自动关闭
                        btnAlign: 'c', //按钮居中对齐
                        btn: ['确定', '取消'],
                        btn1: (index, layero) =>{
                            sessionStorage.setItem('changeData', JSON.stringify(obj.data));
                            layer.close(index);
                            let indexs = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(indexs);
                            // return false; 阻止按钮关闭
                        },
                        btn2: (index, layero) =>{
                            layer.close(index);
                        }
                    });
                }
            }
        })
    });
});